'use strict';

const Controller = require('egg').Controller;
const data = require('../mock/mock.json')
const py = require('js-pinyin')

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }

  //获取列表数据
  async getList() {
    const { ctx, app } = this;
    let data = await app.mysql.select('state')
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  //通过id获取到详情页
  async detail() {
    const { ctx, app } = this;
    let data = await app.mysql.get('state', { id: ctx.request.body.id })
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  //关注
  async addcar() {
    const { ctx, app } = this;
    const { info } = ctx.request.body;
    const data = await app.mysql.get('add', { id: info.id })
    if (data) {
      await app.mysql.update('add', { id: info.id })
    } else {
      await app.mysql.insert('add', { img1: info.img1, word: info.word, name: info.name, id: info.id })
    }
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  //关注列表
  async addlists() {
    const { ctx, app } = this;
    const { size, current } = ctx.request.body;
    const data = await app.mysql.select('add', { limit: size * current })
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }


  async details() {
    const { ctx, app } = this;
    let data = await app.mysql.get('add', { id: ctx.request.body.id })
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  //添加提问
  async quiz() {
    const { ctx, app } = this;
    const { id, word } = ctx.request.body;
    const data = await app.mysql.insert('quiz', { id, word })
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  //渲染添加后的列表
  async quize() {
    const { ctx, app } = this;
    let data = await app.mysql.select('quiz');
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  //添加预约
  async book() {
    const { ctx, app } = this;
    const { id,city,name,phone,number } = ctx.request.body;
    const data = await app.mysql.insert('book', { id, city,name,phone,number })
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  async books(){
    const { ctx, app } = this;
    let data = await app.mysql.select('book');
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }


  //通过id获取到每一条数据，然后通过mysql语句里的delete进行操作，从而达到删除的效果
  async del() {
    const { ctx, app } = this;
    let data = await app.mysql.delete('quiz', { id: ctx.request.body.id })
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  //通过姓名查找
  async search() {
    const { ctx, app } = this;
    const { value } = ctx.request.body
    let data = await app.mysql.query(`select * from state where name like "%${value}%"`)
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  //通过标题搜索
  async searchs() {
    const { ctx, app } = this;
    const { value } = ctx.request.body
    let data = await app.mysql.query(`select * from state where word like "%${value}%"`)
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  //删除
  async delete() {
    const { ctx, app } = this;
    let data = await app.mysql.delete('state', { id: ctx.request.body.id })
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  async watchdel() {
    const { ctx, app } = this;
    let data = await app.mysql.delete('add', { id: ctx.request.body.id })
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  async det() {
    const { ctx } = this;
    let list = data.filter(v => v.level > 1 && v.name !== '市辖区')
    //console.log(py.getFullChars(list[0].name));
    let cityObj = {}, //保存城市的对象
      sortObj = {}
    list.map(v => {
      //获取当前数据的name的拼音的首字母
      let firstWord = py.getFullChars(v.name).slice(0, 1)

      if (cityObj[firstWord]) {
        cityObj[firstWord].push(v)
      } else {
        cityObj[firstWord] = [v]
      }
    })
    //Object.keys: 返回由对象的键组成的新数组
    let wordList = Object.keys(cityObj).sort();
    wordList.map(v => {
      sortObj[v] = cityObj[v]

    })

    ctx.body = ({
      code: 1,
      data: sortObj
    })
  }


  //数据筛选
  async area() {
    const { ctx, app } = this;
    const { select } = ctx.request.body;
    const data = await app.mysql.select('state', { where: { cityone: select } })
    if (data) {
      ctx.body = {
        code: "000",
        data
      }
    } else {
      ctx.body = {
        code: "001",
      }
    }
  }

  async area2() {
    const { ctx, app } = this;
    const { select2 } = ctx.request.body;
    const data = await app.mysql.select('state', { where: { housescreen: select2 } })
    if (data) {
      ctx.body = {
        code: '000',
        data
      }
    } else {
      ctx.body = {
        code: '001'
      }
    }
  }

  async area3() {
    const { ctx, app } = this;
    const { select3 } = ctx.request.body;
    const data = await app.mysql.select('state', { where: { senscs: select3 } })
    if (data) {
      ctx.body = {
        code: '000',
        data
      }
    } else {
      ctx.body = {
        code: '001'
      }
    }
  }


}

module.exports = HomeController;



